package com.gsy.exception;

import java.text.MessageFormat;

/**
* 业务异常
 * @author hthuang
 * @date 2018年6月25日
 */
public class BusinessException extends RuntimeException {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8303808143429891766L;

	public BusinessException(Throwable cause) {
		super(cause);
	}

	/**
	 * 构建业务异常
	 * @param message 异常信息
	 */
	public BusinessException(String message) {
		super(message);
	}

	public BusinessException(String message, Throwable cause) {
		super(message, cause);
	}

	/**
	 * 构建业务异常<br>
	 * <p>({0}{1}world,"he",llo) => helloworld</p> 
	 * @param pattern 异常信息，可使用 模板
	 * @param arguments 数据
	 */
	public BusinessException(String pattern, Object... arguments) {
		this(MessageFormat.format(pattern, arguments));
	}

	public BusinessException(ExceptionEnum exceptionEnum, String filedName, Object filedValue) {
		this(MessageFormat.format(exceptionEnum.getTemplate(), filedName, filedValue));
	}

	public static enum ExceptionEnum {
		UNIQUE("{0}【{1}】已存在"), NULL("{0}{1}为空"), RELATION("{0}{1}存在关联数据"), NO_SAME("{0}{1}不一致");

		private ExceptionEnum(String template) {
			this.template = template;
		}

		private String template;

		public String getTemplate() {
			return template;
		}

		public void setTemplate(String template) {
			this.template = template;
		}

	}
}
